package com.ssy.wireless.task;

import com.ssy.wireless.entity.*;
import com.ssy.wireless.mapper.*;
import com.ssy.wireless.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * Created by cofey on 16/11/23.
 */
@Component
@Slf4j
public class ApDeviceTrafficTask {

    @Autowired
    private ApDeviceMapper apDeviceMapper;
    @Autowired
    private HotspotMapper hotspotMapper;

    @Autowired
    private ApDeviceTrafficMapper apDeviceTrafficMapper;

    @Autowired
    private ApDeviceTrafficStatMapper apDeviceTrafficStatMapper;

    @Autowired
    private HotspotTrafficStatMapper hotspotTrafficStatMapper;

    //每天整点执行一次
    @Scheduled(cron = "0 0 0/1 * * ?")
    @Transactional
    public void exec() {
        Date dateTime = DateUtils.getTime();
        log.info("AP设备流量统计任务,{}开始执行...", DateUtils.formatYMDHMS(dateTime));
        Date beginTime = DateUtils.minuteSubtract(dateTime, 60);
        Date endTime = dateTime;

        DateUtils.YearMonthWeekDayHour yearMonthWeekDay = DateUtils.getYearMonthWeekDay(dateTime);
        int exist = apDeviceTrafficStatMapper.selectHourExist(yearMonthWeekDay.getHour());
        if (exist > 0) {
            log.info("已经存在该时间[" + yearMonthWeekDay.getHour() + "]段的统计数据,无需统计");
            return;
        }

        List<ApDeviceTraffic> traffics = apDeviceTrafficMapper.selectApDeviceTrafficStatForHour(beginTime, endTime);
        if (null == traffics || traffics.size() < 1) {
            log.info("未找到设备流量记录信息,无需统计");
            return;
        }

        traffics.stream().forEach(traffic -> {
            Double upTraffic = traffic.getUpTraffic();
            Double downTraffic = traffic.getDownTraffic();
            ApDevice apDevice = apDeviceMapper.selectByPrimaryKey(traffic.getApDeviceId());
            //AP设备每小时流量
            ApDeviceTrafficStat stat = new ApDeviceTrafficStat();
            stat.setCreateTime(dateTime);
            stat.setUpTraffic(upTraffic);
            stat.setDownTraffic(downTraffic);
            stat.setHourVal(yearMonthWeekDay.getHour());
            stat.setDayVal(yearMonthWeekDay.getDay());
            stat.setWeekVal(yearMonthWeekDay.getWeek());
            stat.setMonthVal(yearMonthWeekDay.getMonth());
            stat.setYearVal(yearMonthWeekDay.getYear());
            stat.setApDeviceId(apDevice.getId());
            stat.setHotspotType(apDevice.getHotspotType());
            stat.setHotspotId(apDevice.getHotspotId());
            stat.setEntId(apDevice.getEntId());
            apDeviceTrafficStatMapper.insert(stat);
        });

        traffics = apDeviceTrafficMapper.selectHostpotTrafficStatForHour(beginTime, endTime);
        if (null == traffics || traffics.size() < 1) {
            log.info("未找到热点流量记录信息,无需统计");
            return;
        }

        traffics.stream().forEach(traffic -> {
            Double upTraffic = traffic.getUpTraffic();
            Double downTraffic = traffic.getDownTraffic();
            Hotspot hotspot = hotspotMapper.selectByPrimaryKey(traffic.getHotspotId());
            HotspotTrafficStat hotspotTrafficStat = new HotspotTrafficStat();
            hotspotTrafficStat.setCreateTime(dateTime);
            hotspotTrafficStat.setUpTraffic(upTraffic);
            hotspotTrafficStat.setDownTraffic(downTraffic);
            hotspotTrafficStat.setHourVal(yearMonthWeekDay.getHour());
            hotspotTrafficStat.setDayVal(yearMonthWeekDay.getDay());
            hotspotTrafficStat.setWeekVal(yearMonthWeekDay.getWeek());
            hotspotTrafficStat.setMonthVal(yearMonthWeekDay.getMonth());
            hotspotTrafficStat.setYearVal(yearMonthWeekDay.getYear());
            hotspotTrafficStat.setHotspotType(hotspot.getType());
            hotspotTrafficStat.setHotspotId(hotspot.getId());
            hotspotTrafficStat.setEntId(hotspot.getEntId());
            hotspotTrafficStatMapper.insert(hotspotTrafficStat);
        });
        log.info("AP设备流量统计任务执行成功");
    }
}
